package descentFly.away3D4.material
{
	import away3d.animators.UVAnimator;
	import away3d.animators.data.UVAnimationFrame;
	import away3d.animators.data.UVAnimationSequence;
	import away3d.arcane;
	import away3d.core.base.SubMesh;
	import away3d.entities.Mesh;
	import away3d.materials.BitmapMaterial;
	
	import flash.display.BitmapData;
	
	public class df_UVAnimMaterial extends BitmapMaterial {
		private var _animator	: UVAnimator;
		private var _clip 		: UVAnimationSequence;
		public function df_UVAnimMaterial(bitmapData:BitmapData=null, smooth:Boolean=true) {
			super(bitmapData, smooth, true, mipmap);
			repeat 		= true;
			bothSides 	= true;
			animateUVs	= true;
		}
		
		protected function createAnimator(mesh: SubMesh): void {
			var i 		: uint;
			
			_clip = new UVAnimationSequence('anim');
			for (i=0; i<10; i++) {
				var k: Number = i/10;
				_clip.arcane::addFrame(new UVAnimationFrame(Math.random() * 0.5, k * 2), 200);
			}
			
			_animator = new UVAnimator(mesh);
			_animator.addSequence(_clip);
		}
		
		public function beginAnimation(mesh: SubMesh): void {
			if (!_animator) createAnimator(mesh);
			_animator.play('anim');
		}
		
		override public function dispose(deep:Boolean):void {
			_animator.stop();
			_animator.disposeAsset();
			super.dispose(deep);
		}		
	}
}